package server.communication.listeners;

import server.Server;
import message.AddServerRequestMessage;
import message.Message;
import message.ServerMessageWrapper;
import communication.MessageListener;
import communication.MessageReceivedEvent;

public class AddServerRequestListener implements MessageListener
{
	private final Server server;
	
	public AddServerRequestListener(Server server)
	{
		this.server = server;
	}
	
	@Override
	public void messageRecieved(MessageReceivedEvent e)
	{
		ServerMessageWrapper message = (ServerMessageWrapper) e.getMessage();
		AddServerRequestMessage innerMessage = (AddServerRequestMessage) message.internalMessage;
		
		server.addServerConnection(message.connectionId, innerMessage.getIpAddress());
	}

	@Override
	public boolean acceptsMessage(Message message)
	{
		if (message.getClass().equals(ServerMessageWrapper.class))
			message = ((ServerMessageWrapper) message).internalMessage;
		
		return message.getClass().equals(AddServerRequestMessage.class);
	}

}
